﻿<Page
    x:Class="WinKee.Pages.DatabasePage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:common="using:WinKee.Common"
    mc:Ignorable="d"
    DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Page.Resources>
        <CollectionViewSource x:Name="PasswordEntries" IsSourceGrouped="True"/>
        <common:EntryListTemplateSelector x:Key="EntryTemplateSelector"/>
        <common:ByFolderViewTemplateSelector x:Key="ByFolderTemplateSelector"/>
    </Page.Resources>
    
    <Page.BottomAppBar>
        <CommandBar ClosedDisplayMode="Minimal">
            <CommandBar.SecondaryCommands>
                <AppBarButton Name="ToFolderViewButton" Label="Folder view" Command="{Binding ToFolderViewCommand}"/>
                <AppBarButton Name="ToListViewButton"  Label="List view" Command="{Binding ToListViewCommand}"/>
                <AppBarButton Label="Switch databases" Command="{Binding SwitchCommand}"/>
                <AppBarButton Label="Settings" Command="{Binding SettingsCommand}"/>
                <AppBarButton Label="Exit" Command="{Binding LockCommand}"/>
                <AppBarButton Label="About WinKee" Command="{Binding ShowAboutCommand}"/>
            </CommandBar.SecondaryCommands>
        </CommandBar>
    </Page.BottomAppBar>
    <Grid x:Name="LayoutRoot">
    	<VisualStateManager.VisualStateGroups>
    		<VisualStateGroup x:Name="MainGroup">
    			<VisualState x:Name="ByNameState">
    				<Storyboard>
    					<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ByFolderGrid">
    						<DiscreteObjectKeyFrame KeyTime="0">
    							<DiscreteObjectKeyFrame.Value>
    								<Visibility>Collapsed</Visibility>
    							</DiscreteObjectKeyFrame.Value>
    						</DiscreteObjectKeyFrame>
    					</ObjectAnimationUsingKeyFrames>
    				</Storyboard>
    			</VisualState>
    			<VisualState x:Name="ByFolderState">
    				<Storyboard>
    					<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ByNameGrid">
    						<DiscreteObjectKeyFrame KeyTime="0">
    							<DiscreteObjectKeyFrame.Value>
    								<Visibility>Collapsed</Visibility>
    							</DiscreteObjectKeyFrame.Value>
    						</DiscreteObjectKeyFrame>
    					</ObjectAnimationUsingKeyFrames>
    				</Storyboard>
    			</VisualState>
    		</VisualStateGroup>
    	</VisualStateManager.VisualStateGroups>
        <Grid.ChildrenTransitions>
            <TransitionCollection>
                <EntranceThemeTransition/>
            </TransitionCollection>
        </Grid.ChildrenTransitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- TitlePanel -->
        <StackPanel Grid.Row="0" Margin="24,17,0,0">
            <TextBlock Text="WinKee" Style="{ThemeResource TitleTextBlockStyle}" Typography.Capitals="SmallCaps"/>
        </StackPanel>

        <Grid Grid.Row="1" Name="ByNameGrid">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <Grid Grid.Row="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <TextBox InputScope="Search" Name="SearchBox"  PlaceholderText="Search" Text="{Binding SearchString, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" GotFocus="SearchBoxGotFocus" IsTextPredictionEnabled="False" />
                <Button HorizontalAlignment="Right" Content="X" Grid.Column="1" Command="{Binding ClearFilterCommand}"/>
            </Grid>

            <Grid Grid.Row="1">
                <SemanticZoom Name="ZoomControl">
                    <SemanticZoom.ZoomedInView>
                        <ListView IsItemClickEnabled="True" SelectionMode="None" ItemClick="ListViewBase_OnItemClick" ItemsSource="{Binding Source={StaticResource PasswordEntries}}" ScrollViewer.IsVerticalScrollChainingEnabled="False" ItemTemplateSelector="{StaticResource EntryTemplateSelector}">
                            <ListView.GroupStyle>
                                <GroupStyle HidesIfEmpty="True">
                                    <GroupStyle.HeaderTemplate>
                                        <DataTemplate>
                                            <Border BorderBrush="{StaticResource PhoneAccentBrush}" BorderThickness="1" Background="{StaticResource PhoneChromeBrush}">
                                                <TextBlock TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Key}" Width="60" FontSize="30" Margin="0,10" FontWeight="Bold" Foreground="{StaticResource PhoneAccentBrush}" Style="{StaticResource ControlHeaderTextBlockStyle}"/>
                                            </Border>
                                        </DataTemplate>
                                    </GroupStyle.HeaderTemplate>
                                </GroupStyle>
                            </ListView.GroupStyle>
                        </ListView>
                    </SemanticZoom.ZoomedInView>
                    <SemanticZoom.ZoomedOutView>
                        <GridView ShowsScrollingPlaceholders="True" ScrollViewer.IsHorizontalScrollChainingEnabled="False" Background="{StaticResource PhoneChromeBrush}">
                            <GridView.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Height="80" Width="80" TextAlignment="Center" Text="{Binding Group.Key}" Foreground="{StaticResource PhoneAccentBrush}" FontWeight="Bold" FontSize="36" />
                                </DataTemplate>
                            </GridView.ItemTemplate>
                            <GridView.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <ItemsWrapGrid Orientation="Horizontal" MaximumRowsOrColumns="4"/>
                                </ItemsPanelTemplate>
                            </GridView.ItemsPanel>
                            <GridView.ItemContainerStyle>
                                <Style TargetType="GridViewItem">
                                    <Setter Property="Margin" Value="2" />
                                    <Setter Property="BorderBrush" Value="{StaticResource PhoneAccentBrush}" />
                                    <Setter Property="BorderThickness" Value="1" />
                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                                    <Setter Property="VerticalContentAlignment" Value="Center" />
                                </Style>
                            </GridView.ItemContainerStyle>
                        </GridView>
                    </SemanticZoom.ZoomedOutView>
                </SemanticZoom>
            </Grid>
        </Grid>

        <Grid Grid.Row="1" Name="ByFolderGrid">
            
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            
            <StackPanel Orientation="Vertical" Grid.Row="0" Margin="5,2,5,0">
                <TextBlock Text="Current path:" FontSize="14" Foreground="{StaticResource PhoneDisabledBrush}"/>
                <TextBlock Text="{Binding Breadcrumb}" FontSize="18" TextTrimming="CharacterEllipsis" Foreground="{StaticResource PhoneAccentBrush}"/>
            </StackPanel>
            
            <ListView Margin="5" Grid.Row="1" IsItemClickEnabled="True" SelectionMode="None" ItemClick="FolderListItemClick" ItemsSource="{Binding CurrentItems}" ItemTemplateSelector="{StaticResource ByFolderTemplateSelector}" />  
        </Grid>
    </Grid>
</Page>
